/*
 * author by Xu
 */
package bgate.font;

import java.io.IOException;
import java.io.InputStream;
import android.graphics.Canvas;

/**
 * 
 * @author chien
 */
public class PiPoDesigner {

	private static byte PiPoRGB[] = null;

	private final static char[] arrChar = { 'a', 'Ấ', 'ể', 'l', 'ơ', 'Ú', '2',
			':', 'ữ', 'á', 'Ầ', 'Ê', 'L', 'ớ', 'Ù', '3', ';', 'Ữ', 'à', 'Ậ',
			'Ế', 'm', 'ờ', 'Ụ', '4', '{', 'õ', 'ạ', 'Ẩ', 'Ề', 'M', 'ợ', 'Ủ',
			'5', '}', 'Õ', 'ả', 'b', 'Ệ', 'n', 'ở', 'ư', '6', '[', 'ỗ', 'A',
			'B', 'Ể', 'N', 'Ơ', 'ứ', '7', ']', 'Ỗ', 'Á', 'c', 'f', 'o', 'Ớ',
			'ừ', '8', '<', 'ỡ', 'À', 'C', 'F', 'ó', 'Ờ', 'ự', '9', '>', 'Ỡ',
			'Ạ', 'd', 'g', 'ò', 'Ợ', 'ử', '!', ',', 'ĩ', 'Ả', 'D', 'G', 'ọ',
			'Ở', 'Ư', '@', '.', 'Ĩ', 'ă', 'đ', 'h', 'ỏ', 'p', 'Ứ', '#', '|',
			'ỵ', 'ắ', 'Đ', 'H', 'O', 'P', 'Ừ', '$', '\\', 'ý', 'ằ', 'e', 'i',
			'Ó', 'q', 'Ự', '%', '`', 'ỳ', 'ặ', 'é', 'í', 'Ò', 'Q', 'Ử', '^',
			'~', 'ỷ', 'ẳ', 'è', 'ì', 'Ọ', 'r', 'v', '&', 'ã', 'ỹ', 'Ă', 'ẹ',
			'ị', 'Ỏ', 'R', 'V', '*', 'Ã', 'Ỵ', 'Ắ', 'ẻ', 'ỉ', 'ô', 's', 'w',
			'(', 'ă', 'Ý', 'Ằ', 'E', 'I', 'ố', 'S', 'W', ')', 'Ẵ', 'Ỳ', 'Ặ',
			'É', 'Í', 'ồ', 't', 'x', '-', 200, 'Ỷ', 'Ẳ', 'È', 'Ì', 'ộ', 'T',
			'X', '_', 'Ẫ', 'Ỹ', 'â', 'Ẹ', 'Ị', 'ổ', 'u', 'y', '+', 'ẽ', 200,
			'ấ', 'Ẻ', 'Ỉ', 'Ô', 'ú', 'Y', '=', 'Ẽ', 200, 'ầ', 'ê', 'j', 'Ố',
			'ù', 'z', '\'', 'ễ', 200, 'ậ', 'ế', 'J', 'Ồ', 'ụ', 'Z', '\"', 'Ễ',
			200, 'ẩ', 'ề', 'k', 'Ộ', 'ủ', '0', '?', 'ũ', 200, 'Â', 'ệ', 'K',
			'Ổ', 'U', '1', '/', 'Ũ', 200 };

	private static byte[] charHeight;
	private static byte[] rgbAlphal = {};

	private static int rgbtemp[];

	public static byte height = 26;

	private static byte[] char_Height = { 12, 22, 19, 14, 14, 21, 15, 13, 17,
			18, 20, 19, 15, 18, 21, 15, 15, 20, 18, 23, 19, 12, 18, 18, 15, 21,
			17, 15, 22, 22, 15, 17, 21, 15, 21, 15, 18, 15, 22, 12, 18, 14, 15,
			19, 18, 15, 15, 21, 15, 17, 17, 15, 19, 22, 20, 12, 15, 12, 20, 17,
			15, 10, 17, 20, 15, 15, 17, 20, 17, 15, 10, 20, 18, 16, 15, 17, 20,
			18, 15, 7, 17, 21, 15, 15, 15, 21, 16, 5, 20, -99, 16, 16, 15, 18,
			15, 20, 18, 20, 15, 20, 15, 15, 15, 15, 20, 17, 17, 25, 20, 12, 16,
			20, 15, 20, 19, 6, 20, 19, 17, 20, 20, 17, 20, 11, 6, 21, 20, 17,
			20, 18, 12, 12, 19, 17, 19, 19, 15, 19, 21, 15, 15, 10, 20, 18, 22,
			18, 18, 16, 12, 12, 17, 20, 20, 22, 15, 15, 19, 15, 14, 17, 23, 20,
			22, 20, 20, 19, 15, 12, 4, 19, 20, 23, 20, 20, 19, 15, 15, 4, 21,
			19, 16, 18, 18, 20, 12, 15, 10, 12, -99, 20, 21, 21, 19, 17, 15, 9,
			20, -99, 20, 16, 19, 19, 17, 12, 7, 19, -99, 19, 19, 15, 23, 12,
			15, 6, 22, -99, 20, 19, 15, 22, 18, 15, 15, 16, -99, 18, 19, 15,
			23, 15, 14, 17, 19, -99

	};

	private static byte[] charWidth = {

	10, 12, 11, 5, 11, 11, 10, 4, 13, 11, 11, 10, 9, 11, 11, 10, 4, 13, 10, 11,
			10, 14, 11, 11, 11, 7, 11, 10, 12, 10, 15, 11, 11, 10, 7, 11, 10,
			11, 10, 11, 11, 13, 10, 6, 12, 11, 11, 12, 12, 12, 13, 10, 6, 11,
			11, 8, 7, 11, 12, 13, 10, 11, 11, 11, 11, 10, 11, 12, 13, 10, 11,
			12, 11, 11, 11, 11, 12, 13, 5, 6, 8, 11, 11, 11, 11, 12, 13, 5, 8,
			1, 10, 12, 11, 11, 11, 13, 14, 4, 12, 10, 12, 11, 11, 11, 13, 10,
			11, 11, 10, 10, 5, 11, 11, 13, 15, 6, 10, 10, 10, 8, 11, 11, 13,
			10, 15, 10, 10, 10, 8, 11, 8, 10, 15, 10, 10, 11, 10, 5, 11, 12,
			11, 10, 11, 11, 11, 10, 6, 11, 9, 14, 7, 10, 11, 11, 10, 5, 12, 11,
			15, 7, 15, 10, 11, 10, 7, 11, 7, 11, 10, 11, 11, 11, 10, 7, 11, 11,
			11, 11, 11, 11, 10, 10, 5, 12, 11, 10, 10, 10, 1, 12, 10, 6, 11,
			11, 11, 11, 10, 1, 10, 10, 7, 11, 11, 9, 4, 10, 1, 10, 12, 8, 12,
			11, 12, 7, 11, 1, 12, 11, 10, 11, 11, 9, 9, 11, 1, 11, 10, 11, 12,
			11, 9, 11, 11, 1

	};

	// color
	public static final int COLOR_BLACK = 0xFF000000;
	public static final int COLOR_YELOW = 0xFFFFFF00;
	public static final int COLOR_PINK = 0xFFFF00FF;
	public static final int COLOR_RED = 0xFFFF0000;
	public static final int COLOR_GREEN = 0xFF00FF00;
	public static final int COLOR_BLUE = 0xFF0000FF;
	public static final int COLOR_WHITE = 0xFFFFFFFF;
	// //////////////////////////////////////////////

	private static final int SUM_CHAR = arrChar.length;
	private static byte width_SPRITE = 15;
	private static byte height_SPRITE = 26;
	private static byte space_Width = 6;
	private static final int SUM_SPRITEINROW = 9;
	private static int leng_RGBARR;
	private static byte line_Height = 26;
	private static byte alphal_Width = 15;
	private static byte alphal_Height = 26;
	private static int rgbb[];

	public static final void init(String pipoFontFile, String charWidthFile,
			String charHeightFile, String charTopFile, String alphalFile,
			String initFile) {
		if (pipoFontFile != null)
			loadFont(pipoFontFile, charWidthFile, charHeightFile, charTopFile,
					alphalFile, initFile);
		else
			loadFont("fonts.pipo", "charwidth.pipo", "charheight.pipo",
					"chartop.pipo", "alpha.pipo", "init.pipo");
		Runtime.getRuntime().gc();
	}

	public static final int getLengString(byte[] b, int color1, int color2,
			int offset, int length) {
		return drawString(null, 0, 0, b, color1, color2, offset, length);
	}

	public static final int drawString(Canvas canvas, int x, int y,
			byte[] index, int color1, int color2, int offset, int length) {
		int lengStr = 0;
		int leng = offset + length;
		if (PiPoRGB == null)
			init(null, null, null, null, null, null);
		for (int i = offset; i < leng; i++) {
			if (index[i] != 127)
				lengStr += drawRGB(canvas, index[i], x + lengStr, y, color1,
						color2);
			else {
				lengStr = 0;
				y += line_Height;
			}
		}
		return lengStr;
	}

	public static final int drawString(Canvas canvas, int x, int y,
			byte[] index, int color1, int color2, int offset, int length,
			int width) {
		int lengStr = 0;
		int dem1 = 0, dem2 = -1;
		int leng = offset + length;

		if (PiPoRGB == null)
			init(null, null, null, null, null, null);

		for (int i = offset; i < leng; i++) {
			if ((index[i] == SUM_CHAR - 127) || (index[i] == 127)) {
				dem1 = dem2 + 1;
				dem2 = i - 1;

				int temp2 = getLengString(index, color1, color2, dem1, dem2
						- dem1 + 1);
				if (temp2 + lengStr <= width) {
					drawString(canvas, x + lengStr, y, index, color1, color2,
							dem1, dem2 - dem1 + 1);
					if (index[i] == SUM_CHAR - 127)
						lengStr += temp2;
					else {
						lengStr = 0;
						y += line_Height;
						dem2 += 1;
					}
				} else {
					y += line_Height;
					dem1 += 1;
					drawString(canvas, x, y, index, color1, color2, dem1, dem2
							- dem1 + 1);
					lengStr = temp2;
				}
			}
			// code here

		}
		if (dem2 != leng - 2) {
			dem1 = dem2 + 1;
			dem2 = leng - 1;
			int temp2 = getLengString(index, color1, color2, dem1, dem2 - dem1
					+ 1);
			if (temp2 + lengStr <= width) {
				drawString(canvas, x + lengStr, y, index, color1, color2, dem1,
						dem2 - dem1 + 1);
				lengStr += temp2;
				y += line_Height;
				dem2 += 1;
			} else {
				y += line_Height;
				dem1 += 1;
				drawString(canvas, x, y, index, color1, color2, dem1, dem2
						- dem1 + 1);
				lengStr = temp2;
			}
		}
		return lengStr;
	}

	public static final byte[] toByteIndex(String str) {
		int length = str.length();
		byte b[] = new byte[length];
		for (int i = 0; i < length; i++) {
			if (str.charAt(i) != '\n')
				b[i] = (byte) (getIndex(str.charAt(i)) - 127);

			else
				b[i] = 127;
		}

		return b;

	}

	private static final void loadFont(String pipoFontFile,
			String charWidthFile, String charHeightFile, String charTopFile,
			String alphalFile, String initFile) {
		InputStream is = PiPoDesigner.class.getResourceAsStream(initFile);
		byte Temp[] = new byte[6];
		try {
			is.read(Temp);
			width_SPRITE = Temp[0];
			height_SPRITE = Temp[1];
			leng_RGBARR = width_SPRITE * height_SPRITE;

			rgbb = new int[leng_RGBARR];

			space_Width = Temp[2];
			line_Height = Temp[3];
			alphal_Width = Temp[4];
			alphal_Height = Temp[5];
			rgbtemp = new int[alphal_Height * alphal_Width];
		} catch (IOException ex) {
			ex.printStackTrace();
		}

		InputStream is2 = PiPoDesigner.class.getResourceAsStream(pipoFontFile);
		try {
			PiPoRGB = new byte[is2.available()];
			is2.read(PiPoRGB);

			is2.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		InputStream is3 = PiPoDesigner.class.getResourceAsStream(charWidthFile);
		try {
			charWidth = new byte[is3.available()];
			is3.read(charWidth);
			is3.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		InputStream is4 = PiPoDesigner.class
				.getResourceAsStream(charHeightFile);
		try {
			char_Height = new byte[is4.available()];
			is4.read(char_Height);
			is4.close();

		} catch (IOException ex) {
			ex.printStackTrace();
		}

		InputStream is5 = PiPoDesigner.class.getResourceAsStream(charTopFile);
		try {
			charHeight = new byte[is5.available()];
			is5.read(charHeight);
			is5.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}

		InputStream is6 = PiPoDesigner.class.getResourceAsStream(alphalFile);
		try {
			rgbAlphal = new byte[is6.available()];
			is6.read(rgbAlphal);
			is6.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}

	}

	public static final int drawCenterString(Canvas canvas, int x, int y,
			byte[] index, int color1, int color2, int offset, int length) {
		return drawString(canvas,
				x - getLengString(index, color1, color2, offset, length) / 2,
				y, index, color1, color2, offset, length);
	}

	private static final int drawRGB(Canvas canvas, int index, int x, int y,
			int color1, int color2) {
		index += 127;
		int leng = space_Width;

		if (index != SUM_CHAR) {
			if ((color1 & (0xff000000)) == 0)
				leng = charWidth[index] - 1;
			else
				leng = charWidth[index];
			if (index != 87) {
				int temp2 = char_Height[index] + charHeight[index];
				int temp = (index % SUM_SPRITEINROW) * width_SPRITE
						+ (index / SUM_SPRITEINROW) * SUM_SPRITEINROW
						* leng_RGBARR + charHeight[index] * SUM_SPRITEINROW
						* width_SPRITE;
				if (canvas != null)
					for (int i = charHeight[index]; i < temp2; i++) {
						for (int j = 0; j < charWidth[index]; j++) {
							switch (PiPoRGB[temp + j]) {
							case 1:
								rgbb[i * width_SPRITE + j] = color1;
								break;
							case 2:
								rgbb[i * width_SPRITE + j] = color2;
								break;
							default:
								rgbb[i * width_SPRITE + j] = 0;
							}
						}
						temp += SUM_SPRITEINROW * width_SPRITE;
					}
				if (canvas != null)
					canvas.drawBitmap(rgbb, charHeight[index] * width_SPRITE,
							width_SPRITE, x, y + charHeight[index],
							charWidth[index], char_Height[index], true, null);

			} else {
				leng = alphal_Width;
				if (canvas != null) {
					for (int i = 0; i < alphal_Width; i++) {
						for (int j = 0; j < alphal_Height; j++)
							switch (rgbAlphal[i * alphal_Width + j]) {
							case 2:
								rgbtemp[i * alphal_Width + j] = color1;
								break;
							case 1:
								rgbtemp[i * alphal_Width + j] = color2;
								break;
							default:
								rgbtemp[i * alphal_Width + j] = 0;
							}
					}
					canvas.drawBitmap(rgbtemp, 0, alphal_Width, x, y,
							alphal_Width, alphal_Height, true, null);

				}
			}

		}
		return leng;
	}

	public static final int getIndex(char ch) {
		int i;
		for (i = 0; i < SUM_CHAR; i++) {
			if (ch == arrChar[i]) {
				break;
			}
		}
		return i;
	}
}
